<?php
namespace Wap\Controller;

use Think\Controller;
use Think\Model;
use User\Model\FactoryModel;
use Wap\Service\WeixinService;
use User\Model\EditorModel;

class EditorController extends WapController
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $id = I("get.id", "-1", "");
        if($id != -1){
            $editor = M( "editor" )->where(['id' => $id, 'status' => EditorModel::STATUS_ACTIVE])->find();
            //分享数据
            $shareData = array();
            if(!empty($editor)) {
                $wx = WeixinService::getWxuser($editor['token']);
                $wxJSApi = WeixinService::buildWXJSApi($wx);
                if (!empty($wxJSApi)) {
                    $data = array(
                        'appId' => $wx['appid'],
                        'nonceStr' => $wxJSApi['noncestr'],
                        'wxTicket' => $wxJSApi['ticket'],
                        'timestamp' => $wxJSApi['timestamp'],
                        'wxSha1' => $wxJSApi['sha1']
                    );
                    $this->assign('shareData', $data);
                }
            }
            $editor['pagehtml'] = $editor['pagehtml'];
            $editor['share_link'] = $editor['share_link']?$editor['share_link']:C('SITE_URL').'/Wap/editor/index?id='.$id;
            $editor['share_image'] = $editor['share_image'] ? C('SITE_URL').'/'.$editor['share_image'] : '';
            $this->assign('editor', $editor);
            $this->assign('show_copyright', 1);
        }
        $this->display( 'index' );
    }

    public function new_index()
    {
        $id = I("get.id", "-1", "");
        if($id != -1){
            $editor = M( "editor" )->where(['id' => $id, 'status' => EditorModel::STATUS_ACTIVE])->find();
            //分享数据
            $shareData = array();
            if(!empty($editor)) {
                $wx = WeixinService::getWxuser($editor['token']);
                $wxJSApi = WeixinService::buildWXJSApi($wx);
                if (!empty($wxJSApi)) {
                    $data = array(
                        'appId' => $wx['appid'],
                        'nonceStr' => $wxJSApi['noncestr'],
                        'wxTicket' => $wxJSApi['ticket'],
                        'timestamp' => $wxJSApi['timestamp'],
                        'wxSha1' => $wxJSApi['sha1']
                    );
                    $this->assign('shareData', $data);
                }
            }
            $editor['pagehtml'] = $editor['pagehtml'];
            $editor['share_link'] = $editor['share_link']?$editor['share_link']:C('SITE_URL').'/Wap/editor/new_index?id='.$id;
            $editor['share_image'] = $editor['share_image'] ? C('SITE_URL').'/'.$editor['share_image'] : '';
            $this->assign('editor', $editor);
        }
        $this->display( 'new_index' );
    }

    public function template()
    {
        $id = I("get.id", "-1", "");
        if($id != -1){
            $editor = M( "editor_template" )->where(['id' => $id, 'status' => EditorModel::STATUS_ACTIVE])->find();
            $this->assign('editor', $editor);
        }
        $this->display( 'template' );
    }

    //编辑器版本3
    public function index3()
    {
        $id = I("get.id", -1);
        if($id == -1){
            header("Content-type: text/html; charset=utf-8");
            die('活动不存在');
        }
        $where['id'] = $id;
        $where['status'] = EditorModel::STATUS_ACTIVE;
        $editor = M("editor")->where($where)->find();
        $wxConfig = array();
        if(!empty($editor)) {
            $title = $editor['title'];
            $content = $editor['pagehtml'];
            $wx = WeixinService::getWxuser($editor['token']);
            $wxJSApi = WeixinService::buildWXJSApi($wx);
            if(!empty($wxJSApi)){
                $wxConfig = array(
                    'appId' => $wx['appid'],
                    'nonceStr' => $wxJSApi['noncestr'],
                    'signature' => $wxJSApi['sha1'],
                    'timestamp' => $wxJSApi['timestamp'],
                );
                $this->assign('wxConfig', $wxConfig);
            }
            $wxShare = array(
                'share_title' => $editor['share_title'] ? $editor['share_title'] : $editor['title'],
                'share_desc' => $editor['share_desc'] ? $editor['share_desc'] : $editor['title'],
                'share_link' => $editor['share_link'] ? $editor['share_link'] : C('SITE_URL').'/Wap/editor/index3?id='.$id,
                'share_image' => $editor['share_image'] ? C('SITE_URL').'/'.$editor['share_image'] : ''
            );
            $this->assign('title', $title);
            $this->assign('content', $content);
            $this->assign('wxConfig', $wxConfig);
            $this->assign('wxShare', $wxShare);
            $this->display('index3');
        }else{
            header("Content-type: text/html; charset=utf-8");
            die('活动不存在');
        }
    }
}